package com.jfinal.ext.util;

/**
 * @author Timor
 * @version 1.0
 * @date 2023/03/15 17:29:28
 */
public class RatioUtil {

    private RatioUtil() {}

    public static String toRatio(long m, long n) {
        if (0 == m || 0 == n) {
            if (m == n) {
                return "1:1";
            }
            return m + ":" + n;
        }

        final long divisor = minCommonDivisor(m, n);

        return (m / divisor) + ":" + (n / divisor);
    }

    /**
     * 求最小公约数
     * @param m m，不为0
     * @param n n，不为0
     * @return 最小公约数
     */
    public static long minCommonDivisor(long m, long n) {
        if (n > m) {
            long temp = n;
            n = m;
            m = temp;
        }

        if (m % n == 0) {
            return n;
        }
        return minCommonDivisor(n, m % n);
    }

}
